//
//  UIView+MSView.m
//  MuRenHe
//
//  Created by mls on 2021/5/23.
//

#import "UIView+MSView.h"

@implementation UIView (MSView)

- (UILabel *)ms_badgeView
{
    UILabel *label = objc_getAssociatedObject(self, _cmd);
    if (!label)
    {
        label = [[UILabel alloc] init];
        [label ms_makeChains:^(MSChainMaker * _Nonnull make) {
            
            make
            .ms_textAlignment(NSTextAlignmentCenter)
            .ms_addToSuperView(self);
        }];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.right.equalTo(self);
        }];
        
        self.ms_badgeView = label;
    }
    return label;
}

- (void)setMs_badgeView:(UILabel *)ms_badgeView
{
    objc_setAssociatedObject(self, @selector(ms_badgeView), ms_badgeView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UILabel *)ms_label
{
    UILabel *label = objc_getAssociatedObject(self, _cmd);
    if (!label)
    {
        label = [[UILabel alloc] init];
        [label ms_makeChains:^(MSChainMaker * _Nonnull make) {
            
            make
            .ms_textAlignment(NSTextAlignmentCenter)
            .ms_addToSuperView(self);
        }];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.edges.equalTo(self);
        }];
        
        self.ms_label = label;
    }
    return label;
}
- (void)setMs_label:(UILabel *)ms_label
{
    objc_setAssociatedObject(self, @selector(ms_label), ms_label, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)setMs_x:(CGFloat)ms_x
{
    CGRect frame = self.frame;
    frame.origin.x = ms_x;
    self.frame = frame;
}

- (CGFloat)ms_x
{
    return self.frame.origin.x;
}

- (void)setMs_y:(CGFloat)ms_y
{
    CGRect frame = self.frame;
    frame.origin.y = ms_y;
    self.frame = frame;
}

- (CGFloat)ms_y
{
    return self.frame.origin.y;
}

- (void)setMs_centerX:(CGFloat)ms_centerX
{
    CGPoint center = self.center;
    center.x = ms_centerX;
    self.center = center;
}

- (CGFloat)ms_centerX
{
    return self.center.x;
}

- (void)setMs_centerY:(CGFloat)ms_centerY
{
    CGPoint center = self.center;
    center.y = ms_centerY;
    self.center = center;
}

- (CGFloat)ms_centerY
{
    return self.center.y;
}

- (void)setMs_width:(CGFloat)ms_width
{
    CGRect frame = self.frame;
    frame.size.width = ms_width;
    self.frame = frame;
}

- (CGFloat)ms_width
{
    return self.frame.size.width;
}

- (void)setMs_height:(CGFloat)ms_height
{
    CGRect frame = self.frame;
    frame.size.height = ms_height;
    self.frame = frame;
}

- (CGFloat)ms_height
{
    return self.frame.size.height;
}

- (void)setMs_size:(CGSize)ms_size
{
//    self.width = size.width;
//    self.height = size.height;
    CGRect frame = self.frame;
    frame.size = ms_size;
    self.frame = frame;
}

- (CGSize)ms_size
{
    return self.frame.size;
}


#pragma mark -
/** 设置label内向偏移量 */
- (void)ms_setLabelEdges:(UIEdgeInsets)edge
{
    [self.ms_label mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.edges.mas_equalTo(edge);
    }];
}
/** 截图 */
- (UIImage *)ms_screenShot
{
    return [self ms_screenShotWithSize:self.bounds.size opaque:NO];
}
/** 根据view生成图片（opaque 表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES.） */
- (UIImage *)ms_screenShotWithSize:(CGSize)size
                            opaque:(BOOL)opaque
{
    // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了，关键就是第三个参数 [UIScreen mainScreen].scale。
    UIGraphicsBeginImageContextWithOptions(size, opaque, [UIScreen mainScreen].scale);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

@end
